package edu.zut.cs.network.iountil;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import edu.zut.cs.network.entity.Book;

public class IOUntil {

	public List<Book> ReadCSVFile(String filepath) {
		List<Book> bookslist = new ArrayList<>();
		BufferedReader in;
		String line = null;
		String[] onerow;
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(filepath), "UTF-8"));
			while ((line = in.readLine()) != null) {
				Book book = new Book();
				onerow = line.split(","); // 默认分割符为逗号，可以不使用逗号
				List<String> onerowlist = Arrays.asList(onerow);
				book.setBookName(onerowlist.get(0));
				book.setPosition(onerowlist.get(1));
				book.setNumber(onerowlist.get(2));
				book.setId(Long.valueOf(onerowlist.get(3)));
				bookslist.add(book);
			}
		} catch (NumberFormatException | IOException e) {
			e.printStackTrace();
		}
		return bookslist;
	}
}
